package com.faithnmn.radio.common;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;

public class PowerUtils {
    public static boolean isConnected(Context context) {
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    }
}

//Set up a BroadcastReceiver for ACTION_BATTERY_CHANGED. An Intent extra will tell you what the charging state is -- see BatteryManager for details.
//
//<application android:icon="@drawable/icon" android:label="@string/app_name">
//     <receiver android:name=".receiver.PlugInControlReceiver">
//         <intent-filter>
//             <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
//             <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
//         </intent-filter>
//     </receiver>
// </application>
//Then
//
//public void onReceive(Context context , Intent intent) {
// String action = intent.getAction();
//
// if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
//     // Do something when power connected
// }
// else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
//     // Do something when power disconnected
// }
//}